<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseItemBase extends Base {
	var $table = '';
	var $prefix = '';
	var $pkey = 'id';
	var $fkey = '';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = true;
	var $_table_status_value = '1';
	var $_table_link_usable = false;
	var $_table_link_gfield = 'name';
	
	//
	var $format = true;
	var $format_check = false;
	var $format_prefix = 'field_';
	var $item_link = false;
	var $item_link_owner = false;
	var $item_fields = array('id', 'name', 'link');
	var $list_fields = array();
	var $search_fields = array('name');
	var $search_type = 'or';
	var $sort_order = false;
	var $sort_fields = array('sort_order+', 'id+');
	var $sort_group = false;
	var $sort_groups = array('name');
	var $sort_query = array();
	var $filter_fields = array();
	var $filter_map = array();
	var $filter_query = array();
	
	//
	var $filters = array();
	var $triggers = array();
	
	//constructor
	function BaseItemBase($table = '') {
		parent::Base();
		
		if( $table != '' ) {
			$this->table = $table;
		}
	}
	
	function setProperty($name, $value = '') {
		if( is_array($name) ) {
			foreach($name as $k => $v) {
				$this->$k = $v;
			}
		} else {
			$this->$name = $value;
		}
	}
	
	function getProperty($name) {
		if( isset($this->$name) ) {
			return $this->$name;
		}
		
		return NULL;
	}
	
	function property($data) {
		if( is_array($data) ) {
			return $this->setProperty($data);
		} else {
			return $this->getProperty($data);
		}
	}
	
	function _pk($check = false) {
		if( $check == true && $this->format == true ) {
			return $this->pkey;
		}
		
		return $this->prefix . $this->pkey;
	}
	
	function _pf($field, $check = false) {
		if( $check == true && $this->format == true ) {
			return $field;
		}
		
		return $this->prefix . $field;
	}
	
	function setFilterQuery($name, $value = '') {
		if( is_array($name) ) {
			$this->filter_query = $name;
		} else {
			$this->filter_query[$name] = $value;
		}
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_field_str = '*';
		if( is_array($this->list_fields) && sizeof($this->list_fields) > 0 ) {
			$query_field_str = implode(', ', $this->list_fields);
		}
		
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		
		$query_group_str = "";
		if( $this->sort_group == true && !empty($this->sort_groups) ) {
			$query_group_str .= " group by ";
			foreach($this->sort_groups as $k => $v) {
				$query_group_str .= $k . ", ";
			}
			$query_group_str = substr($query_group_str, 0, -2);
		}
		
		$query_order_str = "";
		if( $this->sort_order == true ) {
			$this->_get_query_order_sort();
			if( is_array($this->sort_fields) ) {
				$query_order_str = " order by ";
				$i = 0;
				foreach($this->sort_fields as $k => $v) {
					if( substr($v, 0, 1) == '=' ) {
						$v = substr($v, 1);
					} else {
						$v = $this->prefix . $v;
					}
					if( substr($v, -1) == '-' ) {
						$as = 'desc';
						$v = substr($v, 0, -1);
					} elseif( substr($v, -1) == '+' ) {
						$as = 'asc';
						$v = substr($v, 0, -1);
					} else {
						$as = 'asc';
					}
					if( $i > 0 ) {
						$query_order_str .= ", ";
					}
					$query_order_str .= " " . $v . " " . $as;
					$i++;
				}
			} else {
				$query_order_str = " order by " . $this->prefix . "sort_order asc";
			}
		}
		
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query = "select " . $query_field_str . " from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. $query_str . $query_group_str . $query_order_str . $query_limit_str;
		$rows = $this->adb->getRows($query);
		
		if( $this->format == true ) {
			for($i=0, $n=sizeof($rows); $i<$n; $i++) {
				$rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
			}
		}
		
		return $rows;
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		
		return $this->adb->getCountA($this->table, " " . $this->_query_owner_rt() . " " . $query_str . " ");
	}
	
	function getItemsStats($filter = '', $check = 0, $type = 'count', $field = '+id') {
		if( !in_array($type, array('count', 'sum', 'min', 'max', 'avg')) ) {
			return false;
		}
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		
		$query_group_str = "";
		if( $this->sort_group == true && !empty($this->sort_groups) ) {
			$query_group_str .= " group by ";
			foreach($this->sort_groups as $k => $v) {
				$query_group_str .= $k . ", ";
			}
			$query_group_str = substr($query_group_str, 0, -2);
		}		
		
		if( $field == '' ) {
			$field = '*';
		} elseif( substr($field, 0, 1) == '=' ) {
			$field = substr($field, 1);
		} elseif( substr($field, 0, 1) == '+' ) {
			$field = $this->prefix . substr($field, 1);
		} elseif( substr($field, 0, 1) == '-' ) {
			$field = $this->prefix . substr($field, 1);
		} elseif( substr($field, 0, 1) == '_' ) {
			$field = $this->prefix . substr($field, 1);
		} else {
			$field = $this->prefix . substr($field, 1);
		}
		
		$query = "select " . $type . " (" . $field . ") as count from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. $query_str . $query_group_str;
		
		return $this->adb->getCount($query);
	}
	
	function getItemsIDs($filter = '', $check = 0, $limit = true) {
		$items = $this->getItems($filter, $check, $limit);
		$pf = $this->prefix;
		if( $this->format == true ) {
			$pf = '';
		}
		$pk = $pf . 'id';
		$ids = array();
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$ids[] = $items[$i][$pk];
		}
		
		return $ids;
	}
	
	function getItemsFormat($filter = '', $check = 0, $limit = true, $field = '') {
		$items = $this->getItems($filter, $check, $limit);
		$pf = $this->prefix;
		if( $this->format == true ) {
			$pf = '';
		}
		$itemsf = array();
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$f1 = $pf . 'id';
			$f2 = $pf . 'id';
			$f3 = 0;
			if( is_array($field) && sizeof($field) >= 2 ) {
				$f1 = $field['0'];
				$f2 = $field['1'];
				if( substr($f1, 0, 1) == '=' ) {
					$f1 = substr($f1, 1);
				} elseif( substr($f1, 0, 1) == '_' ) {
					$f1 = $pf . substr($f1, 1);
				} else {
					$f1 = $pf . $f1;
				}
				if( substr($f2, 0, 1) == '=' ) {
					$f2 = substr($f2, 1);
				} elseif( substr($f2, 0, 1) == '_' ) {
					$f2 = $pf . substr($f2, 1);
				} else {
					$f2 = $pf . $f2;
				}
			} elseif( $field != '' ) {
				$f2 = $field;
				if( substr($f2, 0, 1) == '=' ) {
					$f2 = substr($f2, 1);
				} elseif( substr($f2, 0, 1) == '_' ) {
					$f2 = $pf . substr($f2, 1);
				} else {
					$f2 = $pf . $f2;
				}
			} elseif( isset($items[$i][$pf . 'name']) && strlen($items[$i][$pf . 'name']) > 0 ) {
				$f2 = $pf . 'name';
			} elseif( isset($items[$i][$pf . 'subject']) && strlen($items[$i][$pf . 'subject']) > 0 ) {
				$f2 = $pf . 'subject';
			}
			$itemsf[$items[$i][$f1]] = $items[$i][$f2];
		}
		
		return $itemsf;
	}
	
	function getLatestItems($limit_length = 5) {
		if( $limit_length <= 0 ) {
			$limit_length = 5;
		}
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. " order by " . $this->prefix . "id desc " 
				. " limit " . (int)$limit_length;
		
		return $this->adb->getRows($query);
	}
	
	//
	function checkItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
			if( isset($this->item_fields[$type]) ) {
				$field = $this->item_fields[$type];
				if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
					$field = '_' . $field;
				}
			} elseif( in_array($type, $this->item_fields) ) {
				$field = '_' . $type;
			}
		}
		if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
			$field = $type;
		} elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
			$field = '_' . $type;
			if( $type == 'id' ) {
				$value = intval($value);
			}
		} else {
			return false;
		}
		if( substr($field, 0, 1) == '_' ) {
			$field = $this->prefix . substr($field, 1);
		} elseif( substr($field, 0, 1) == '=' ) {
			$field = substr($field, 1);
		} else {
			$field = $this->prefix . $field;
		}
		
		$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() 
				. " and " . $field . " = '" . $value . "'";
		
		return $this->adb->checkCount($query);
	}
	
	function getItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$this->doEvents("getItemBefore", $value, $type, $check);
		if( $check == -1 ) {
			//
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
			if( isset($this->item_fields[$type]) ) {
				$field = $this->item_fields[$type];
				if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
					$field = '_' . $field;
				}
			} elseif( in_array($type, $this->item_fields) ) {
				$field = '_' . $type;
			}
		}
		if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
			$field = $type;
		} elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
			$field = '_' . $type;
			if( $type == 'id' ) {
				$value = intval($value);
			}
		} else {
			return false;
		}
		if( substr($field, 0, 1) == '_' ) {
			$field = $this->prefix . substr($field, 1);
		} elseif( substr($field, 0, 1) == '=' ) {
			$field = substr($field, 1);
		} else {
			$field = $this->prefix . $field;
		}
		$query_str .= " and " . $field . " = '" . $value . "'";
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
		
		$item = $this->adb->getRow($query);
		if( $this->format == true ) {
			$item = $this->_post_field($item, '', $this->prefix, $this->format_check);
		}
		$this->doEvents("getItemAfter", $value, $type, $check, $item);
		$item = $this->doEvents("formatItem", $item);
		
		return $item;
	}
	
	function getItemBy($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$this->doEvents("getItemBefore", $value, $type, $check);
		if( $check == -1 ) {
			//
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		if( in_array($type, array('id', 'link')) ) {
			$field = $this->prefix . $type;
		} elseif( substr($type, 0, 1) == '=' ) {
			$field = substr($type, 1);
		} else {
			$field = $this->prefix . $type;
		}
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. " and " . $field . " = '" . $value . "'";
		
		$item = $this->adb->getRow($query);
		if( $this->format == true ) {
			$item = $this->_post_field($item, '', $this->prefix, $this->format_check);
		}
		$this->doEvents("getItemAfter", $value, $type, $check, $item);
		$item = $this->doEvents("formatItem", $item);
		
		return $item;
	}
	
	function getPreviousItem($value, $type = 'id', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		$query_order_str = " order by " . $this->prefix . "id desc ";
		$query_limit_str = " limit 1";
		
		if( $type != 'id' ) {
			$item = $this->getItem($value, $type, -1);
			if( $this->format == true ) {
				$value = $item['id'];
			} else {
				$value = $item[$this->prefix . 'id'];
			}
		}
		if( $value > 0 ) {
			$query = "select * from " . $this->table . " where " . $this->prefix . "id < '" . (int)$value . "' " 
					. $query_str . $query_order_str . $query_limit_str;
			
			$item = $this->adb->getRow($query);
			if( $this->format == true ) {
				$item = $this->_post_field($item, '', $this->prefix, $this->format_check);
			}
			
			return $item;
		}
		
		return false;
	}
	
	function getNextItem($value, $type = 'id', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		$query_order_str = " order by " . $this->prefix . "id asc ";
		$query_limit_str = " limit 1";
		
		if( $type != 'id' ) {
			$item = $this->getItem($value, $type, -1);
			if( $this->format == true ) {
				$value = $item['id'];
			} else {
				$value = $item[$this->prefix . 'id'];
			}
		}
		if( $value > 0 ) {
			$query = "select * from " . $this->table . " where " . $this->prefix . "id > '" . (int)$value . "' " 
					. $query_str . $query_order_str . $query_limit_str;
			
			$item = $this->adb->getRow($query);
			if( $this->format == true ) {
				$item = $this->_post_field($item, '', $this->prefix, $this->format_check);
			}
			
			return $item;
		}
		
		return false;
	}
	
	function addItem($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$this->doEvents("addItemBefore", $data_array);
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		//
		$data_array = $this->doEvents("addItemFilter", $data_array);
		//
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			$data_array[$p . $this->_table_owner_field] = $this->owner;
		}
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = $this->_table_status_value;
		
		//$items_id = $this->adb->insertA($this->table, $data_array);
		$items_id = $this->_insert($this->table, $data_array);
		$this->doEvents("addItemAfter", $data_array, $items_id);
		
		//
		if( $items_id > 0 && $this->item_link == true ) {
			$name = isset($data_array[$this->prefix . 'subject']) ? $data_array[$this->prefix . 'subject'] : '';
			if( $name == '' ) {
				$name = isset($data_array[$this->prefix . 'name']) ? $data_array[$this->prefix . 'name'] : '';
			}
			$this->_generate_item_link($items_id, $name, $data_array[$this->prefix . 'link']);
		}
		
		return $items_id;
	}
	
	function addItems($data_array) {
		$items_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$items_ids[] = $this->addItem($v);
			}
		}
		
		return $items_ids;
	}
	
	function addItemsB($data_array) {
		$items_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				if( isset($v[$this->prefix . 'id']) && $v[$this->prefix . 'id'] > 0 ) {
					$id = $v[$this->prefix . 'id'];
					unset($v[$this->prefix . 'id']);
					
					$this->updateItem($id, $v);
					$items_ids[] = $id;
				} else {
					$items_ids[] = $this->addItem($v);
				}
			}
		}
		
		return $items_ids;
	}
	
	function updateItem($items_id, $data_array, $params = '') {
		if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$this->doEvents("updateItemBefore", $items_id, $data_array);
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		//
		$data_array = $this->doEvents("updateItemFilter", $items_id, $data_array);
		//
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		$params = ($params == '') ? "" : " and " . $params;
		$params = "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'" . $params;
		//$result = $this->adb->updateA($this->table, $data_array, $params);
		$result = $this->_update($this->table, $data_array, $params);
		$this->doEvents("updateItemAfter", $data_array, $items_id, $result);
		
		return $result;
	}
	
	function updateItems($items_ids, $data_array, $params = '') {
		if( is_array($items_ids) ) {
			$params = ($params == '') ? "" : " and " . $params;
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'" . $params);
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")" . $params);
			}
		}
	}
	
	function updateItemsF($items_ids, $field, $value = '') {
		$fs = array(
				'top' => array('flag_top', '1'), 
				'recommend' => array('flag_recommend', '1'), 
				'untop' => array('falg_top', '0'), 
				'unrecommend' => array('flag_recommend', '0'), 
				'statusopen' => array('status', '1'), 
				'statusclose' => array('status', '0'),
					);
		if( $field && isset($fs[$field]) ) {
			$data_array = array($this->prefix . $fs[$field]['0'] => $fs[$field]['1']);
			
			return $this->updateItems($items_ids, $data_array);
		}
		return false;
	}
	
	function updateItemsCategory($items_ids, $categories_id) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				return $this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id . "' where " . $this->prefix . "id in (" . $query_ids . ")");
			}
		}
	}
	
	function updateCategoryItemstoNewCategory($categories_id, $categories_id_new) {
		return $this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id_new . "' where " . $this->prefix . "cid = '" . (int)$categories_id . "'");
	}
	
	function updateItemsByFilter($data_array, $filter = '', $check = 0) {
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		
		$check = $this->getItemsTotal($filter, $check);
		if( $check == 0 ) {
			return false;
		}
		
		return $this->_update($this->table, $data_array, " " . $this->_query_owner_rt() . " " . $query_str . " ");
	}
	
	function deleteItem($items_id) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$this->doEvents("deleteItemBefore", $items_id);
		$this->adb->deleteA($this->table, " " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
		$this->doEvents("deleteItemAfter", $items_id);
		
		return true;
	}
	
	function deleteItems($items_ids) {
		if( is_string($items_ids) && strpos($items_ids, ',') !== false ) {
			$items_ids = explode(',', $items_ids);
		}
		$this->doEvents("deleteItemsBefore", $items_ids);
		if( is_array($items_ids) ) {
			$this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
		}
		$this->doEvents("deleteItemsAfter", $items_ids);
	}
	
	function setItemsStatus($items_ids, $status = 1) {
		if( is_array($items_ids) ) {
			$this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
		}
	}
	
	//
	function check($items_id) {
		$count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
		
		return ($count > 0) ? true : false;
	}
	
	function get($items_id) {
		return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function add($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = $this->_table_status_value;
		
		return $this->adb->insertA($this->table, $data_array);
	}
	
	function update($items_id, $data_array) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function delete($items_id) {
		return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	//private
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			if( is_array($this->filters) ) {
				foreach($this->filters as $k => $v) {
					$filter[$k] = $v;
				}
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				if( is_array($this->search_fields) ) {
					if( $this->search_type != 'or' ) {
						$this->search_type = 'and';
					}
					$i = 0;
					$query_str .= " and ( ";
					foreach($this->search_fields as $k => $field) {
						if( $i > 0 ) {
							$query_str .= " " . $this->search_type . " ";
						}
						$query_str .= " " . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
						$i++;
					}
					$query_str .= " ) ";
				} elseif( is_string($this->search_fields) ) {
					$query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
				}
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and " . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
				}
			}
			if( isset($filter['parent']) && $this->_not_null($filter['parent']) == true ) {
				if( is_array($filter['parent']) ) {
					$query_str .= " and " . $this->prefix . "parent in (" . $this->_implode(', ', $filter['parent']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "parent = '" . (int)$filter['parent'] . "' ";
				}
			}
			if( isset($filter['category']) ) {
				if( is_array($filter['category']) ) {
					$query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
			if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
				$query_str .= " and categories_id = '0' ";
			}
			if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
				$qt = $this->_get_query_type();
				foreach($filter as $k => $v) {
					if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
						$k = $this->filter_map[$k];
					}
					if( in_array($k, $this->filter_fields) ) {
						$t = '';
						$p = $this->prefix;
						if( strpos($k, ':') !== false ) {
							$ks = explode(':', $k);
							$k = $ks['0'];
							$t = $ks['1'];
							if( sizeof($ks) > 2 ) {
								$p = $ks['2'];
							}
						}
						if( substr($k, 0, 1) == '=' ) {
							$k = substr($k, 1);
							$p = '';
						}
						if( !in_array($t, $qt) ) {
							$t = '';
						}
						if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
							if( $v === 'now()' || $v === 'now' ) {
								$query_str .= " and " . $p . $k . " " . $t . " now() ";
							} else {
								$query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
							}
						} elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
							if( is_array($v) ) {
								$t1 = substr($t, 0, 1);
								$t2 = substr($t, 1, 1);
								if( $t1 == '[' ) {
									$t1 = '<=';
								} elseif( $t1 == '(' ) {
									$t1 = '<';
								}
								if( $t2 == '[' ) {
									$t2 = '<=';
								} elseif( $t1 == '(' ) {
									$t2 = '<';
								}
								
								$query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
								$query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
							}
						} elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
							if( $t == 'notlike' ) {
								$query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
							} else {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							}
						} else {
							if( $v === 'now()' || $v === 'now' ) {
								$query_str .= " and " . $p . $k . " < now() ";
							} elseif( is_array($v) && sizeof($v) > 0 ) {
								$query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
							} elseif( is_int($v) ) {
								$query_str .= " and " . $p . $k . " = '" . $v . "' ";
							} elseif( is_numeric($v) ) {
								$query_str .= " and " . $p . $k . " = '" . $v . "' ";
							} elseif( is_string($v) ) {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							} else {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							}
						}
					}
				}
			}
		}
		//
		$query_str .= $this->_get_query_str_custom($filter, $check);
		
		return $query_str;
	}
	
	function _get_query_str_custom($filter = '', $check = 0) {
		return "";
	}
	
	function _get_query_order_sort($order_by = '', $order_sort = '') {
		if( $order_by == '' ) {
			$order_by = $this->query_order_by;
		}
		if( $order_sort == '' ) {
			$order_sort = $this->query_order_sort;
		}
		if( $order_by != '' && $order_sort != '' ) {
			if( $order_by == 'dateadded' ) {
				$order_by = 'date_added';
			} elseif( $order_by == 'sortorder' ) {
				$order_by = 'sort_order';
			}
			//
			$sort_query = array();
			if( !is_array($this->sort_query) && is_array($this->sort_fields) ) {
				foreach($this->sort_fields as $k => $v) {
					if( substr($v, -1, 1) == '+' || substr($v, -1, 1) == '-' ) {
						$sort_query[] = substr($v, 0, -1);
					}
				}
			} else {
				$sort_query = $this->sort_query;
			}
			if( is_array($sort_query) && in_array($order_by, $sort_query) ) {
				$sort_fields = array($order_by . ($order_sort == 'desc' ? '+' : '-'));
				$this->sort_fields = $sort_fields;
			}
		}
	}
	
	function _query_owner_rt($pf = '') {
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			if( $pf != '' ) {
				$p = $pf . '.' . $p;
			}
			
			return $this->_query_owner($p . $this->_table_owner_field);
		}
		
		return " 1 ";
	}
	
	function _get_query_type() {
		$query_type = array(
				'=', '>', '>=', '<', '<=', '!=',
				'[]', '()', '[)', '(]',
				'like', 'notlike', '%like%', '%like', 'like%',
				'regexp', 'notregexp',
					);
		return $query_type;
	}
	
	//
	function _generate_item_link($items_id, $items_name = '', $items_link = '') {
		if( $items_link == '' && $items_name != '' ) {
			$items_link = $this->_filter_link($items_name);
		}
		if( $this->_not_null($items_link) ) {
			$items_link = func_db_input($items_link);
			if( $this->item_link_owner == true ) {
				$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " " 
						. " and " . $this->prefix . "id != '" . (int)$items_id . "' " 
						. " and " . $this->prefix . "link = '" . $items_link . "'";
			} else {
				$query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' " 
						. " and " . $this->prefix . "link = '" . $items_link . "'";
			}
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateItem($items_id, array('_link' => $items_link));
				return true;
			}
		}
		$items_link = $this->_filter_link($items_name);
		$items_link = $this->_rand_link($items_id, $items_link);
		
		$this->_generate_item_link($items_id, $items_name, $items_link);
		
		return true;
	}
	
	//
	//Event
	function doEvents() {
		$_args = func_get_args();
		$_argc = func_num_args();
		if( $_argc == 0 ) {
			return false;
		}
		$method = array_shift($_args);
		$params = $_args;
		$method = 'on' . ucfirst($method);
		if( method_exists($this, $method) ) {
			//if( $params == '' ) {
			//	$this->$method();
			//} else {
			//	$this->$method($params);
			//}
			if( $_argc == 1 ) {
				$result = call_user_func_array(array($this, $method));
			} elseif( strstr($method, 'Filter') !== false && sizeof($params) == 1 ) {
				//$result = call_user_func_array(array($this, $method), array(&$params['0']));
				$result = $this->$method($params['0']);
			} else {
				$result = call_user_func_array(array($this, $method), $params);
			}
		}
		
		return $result;
	}
	
	function doTrigger($trigger, $params) {
		if( $trigger['type'] == 'cm' ) {
			if( $trigger['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']) ) {
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']);
			}
			$cmObj = new $trigger['class_name']();
			$vs = call_user_func_array(array($cmObj, $trigger['method']), $params);
		} elseif( $trigger['type'] == 'm' ) {
			$vs = call_user_func_array(array($this, $trigger['method']), $params);
		} elseif( $trigger['type'] == 'f' ) {
			$vs = call_user_func_array($trigger['function'], $params);
		}	
	}
	
	function onAddItemBefore() {
		return true;
	}
	
	function onAddItemFilter($data_array) {
		$fb = $this->onAddItemFilterBind($data_array);
		if( is_array($fb) ) {
			foreach($fb as $k => $v) {
				if( substr($k, 0, 1) == '_' ) {
					$data_array[$this->prefix . substr($k, 1)] = $v;
				} elseif( substr($k, 0, 1) == '=' ) {
					$data_array[substr($k, 1)] = $v;
				} else {
					$data_array[$k] = $v;
				}
			}
		}
		//
		$fc = $this->onAddItemFilterClean($data_array);
		if( is_array($fc) ) {
			foreach($fc as $k => $v) {
				if( substr($k, 0, 1) == '_' ) {
					$k = $this->prefix . substr($k, 1);
				} elseif( substr($k, 0, 1) == '=' ) {
					$k = substr($k, 1);
				}
				if( isset($data_array[$k]) ) {
					unset($data_array[$k]);
				}
			}
		}
		
		return $data_array;
	}
	
	function onAddItemFilterBind($data_array) {
		return false;
	}
	
	function onAddItemFilterClean($data_array) {
		return false;
	}
	
	function onAddItemAfter($data_array, $items_id) {
		if( isset($this->triggers['additemafter']) ) {
			$trigger = $this->triggers['additemafter'];
			$params = func_get_args();
			
			if( $trigger['type'] == 'cm' ) {
				if( $trigger['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']) ) {
					include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']);
				}
				$cmObj = new $trigger['class_name']();
				$vs = call_user_func_array(array($cmObj, $trigger['method']), $params);
			} elseif( $trigger['type'] == 'm' ) {
				$vs = call_user_func_array(array($this, $trigger['method']), $params);
			} elseif( $trigger['type'] == 'f' ) {
				$vs = call_user_func_array($trigger['function'], $params);
			}
		}
		
		return true;
	}
	
	function onUpdateItemBefore($items_id, $data_array) {
		return true;
	}
	
	function onUpdateItemFilter($items_id, $data_array) {
		$fb = $this->onUpdateItemFilterBind($items_id, $data_array);
		if( is_array($fb) ) {
			foreach($fb as $k => $v) {
				if( substr($k, 0, 1) == '_' ) {
					$data_array[$this->prefix . substr($k, 1)] = $v;
				} elseif( substr($k, 0, 1) == '=' ) {
					$data_array[substr($k, 1)] = $v;
				} else {
					$data_array[$k] = $v;
				}
			}
		}
		//
		$fc = $this->onUpdateItemFilterClean($items_id, $data_array);
		if( is_array($fc) ) {
			foreach($fc as $k => $v) {
				if( substr($k, 0, 1) == '_' ) {
					$k = $this->prefix . substr($k, 1);
				} elseif( substr($k, 0, 1) == '=' ) {
					$k = substr($k, 1);
				}
				if( isset($data_array[$k]) ) {
					unset($data_array[$k]);
				}
			}
		}
		
		return $data_array;
	}
	
	function onUpdateItemFilterBind($items_id, $data_array) {
		return false;
	}
	
	function onUpdateItemFilterClean($items_id, $data_array) {
		return false;
	}
	
	function onUpdateItemAfter($items_id, $data_array, $result) {
		if( isset($this->triggers['updateitemafter']) ) {
			$trigger = $this->triggers['updateitemafter'];
			$params = func_get_args();
			
			$this->doTrigger($trigger, $params);
		}
		return true;
	}
	
	function onDeleteItemBefore($items_id) {
		return true;
	}
	
	function onDeleteItemAfter($items_id) {
		return true;
	}
	
	function onGetItemBefore($value, $type, $check) {
		return true;
	}
	
	function onGetItemAfter($value, $type, $check, $item) {
		return true;
	}
	
	function onGetItemsBefore() {
		return true;
	}
	
	function onGetItemsAfter() {
		return true;
	}
	
	function onFormatItem($item) {
		return $item;
	}
}
//
?>